<include file="Public/header" /> 

<script type="text/javascript">
	var main_index=0+<?php echo count($menus['menu']['button'])?>;
	
	//添加主菜单
	function addMenu() {
		var leht=$(".table-menu tbody tr.main").length;
		if(leht >= 3) {
            layer.alert('顶级自定义菜单最多添加3个',{icon: 2});
			return;
		}
		main_index++;
		var input_index = Math.floor(Math.random()*10000);
		var html='';
		html +='<tr class="main" main-index="'+main_index+'">';
		html +='<td><input name="" class="form-control input-medium name" type="text" value=""><a  href="javascript:;" onclick="addSubMenu($(this))"><i class="glyphicon glyphicon-plus"></i> 添加二级菜单</a></td>';
		html +='<td>';
		html +='<select class="form-control" name="type'+main_index+'" >';
		html +='<option value="click">关键字</option>';
		html +='<option value="view">链接跳转</option>';
		html +='<option value="scancode_waitmsg">扫码带提示</option>';
		html +='<option value="scancode_push">扫码推事件</option>';
		html +='<option value="pic_sysphoto">系统拍照发图</option>';
		html +='<option value="pic_photo_or_album">拍照或者相册发图</option>';
		html +='<option value="pic_weixin">微信相册发图</option>';
		html +='<option value="location_select">发送位置</option>';
		html +='</select>';
		//html +='<label  class="radio inline"><input  type="radio" name="type'+main_index+'"   value="click"  checked="true">关键字</label>';
		//html +='<label  class="radio inline"><input  type="radio" name="type'+main_index+'"   value="view" >链接跳转</label>';
		html +='</td>';
		html +='<td class="td-keyword" ><div class="form-inline">';
		html +='<input type="text"  value="" class="form-control input'+leht+' " style="width:335px" /> ';
		/*html +='<button onclick="SelectContractInfo(this);return false;" class="btn btn-default" id="btn'+input_index+'" style="display:none">快捷选择</button></div>';*/
		html +='</td><td>';
		html +='<a href="javascript:;"  onclick="moveMenu($(this),\'up\')"><i class="glyphicon glyphicon-arrow-up"></i></a> ';
		html +='<a href="javascript:;" onclick="moveMenu($(this),\'down\')"><i class="glyphicon glyphicon-arrow-down"></i></a> ';
		html +='<a href="javascript:;" onclick="delMenu($(this))" ><i class="glyphicon glyphicon-remove"></i></a>';
		html +='</td></tr>';
		
		$('.table-menu tbody').append(html);
		sehelctonbin();
	}
	//添加子菜单
	function addSubMenu(o){
		var mainindex =$(o).parent().parent().attr('main-index');
		
		var lengh = $('tr[parent-index='+mainindex+']').length;
		if( lengh >= 5){
           	layer.alert("子菜单最多添加5个",{icon: 2});
			return;
		}
		var input_index = Math.floor(Math.random()*10000);
		$(o).parent().parent().find('td:eq(1)').html('');
		$(o).parent().parent().find('td:eq(2)').html('');
		var html='';
		html +='<tr class="sub" parent-index="'+mainindex+'">';
		html +='<td><div class="sub-node"><input  class="form-control input-medium name" type="text" value=""></div></td>';	
		html +='<td>';
		html +='<select class="form-control" name="type'+lengh+"-"+mainindex+'" >';
		html +='<option value="click">关键字</option>';
		html +='<option value="view">链接跳转</option>';
		html +='<option value="scancode_waitmsg">扫码带提示</option>';
		html +='<option value="scancode_push">扫码推事件</option>';
		html +='<option value="pic_sysphoto">系统拍照发图</option>';
		html +='<option value="pic_photo_or_album">拍照或者相册发图</option>';
		html +='<option value="pic_weixin">微信相册发图</option>';
		html +='<option value="location_select">发送位置</option>';
		html +='</select>';
		//html +='<label class="radio inline"><input  type="radio" name="type'+lengh+"-"+mainindex+'"  value="click" checked="true">关键字</label>';
		//html +='<label class="radio inline"><input  type="radio" name="type'+lengh+"-"+mainindex+'"  value="view">链接跳转</label>';
		html +='</td>';
		html +='<td class="td-keyword"><div class="form-inline">';
		html +='<input class="form-control input-medium keyword" type="text" value="" style="width:335px" >';
		/*html +=' <button onclick="SelectContractInfo(this);return false;" class="btn btn-default" id="btn_'+mainindex+'_'+input_index+'" style="display:none">快捷选择</button></div>';*/
		html +='</td>';
		html +='<td>';
		html +='<a href="javascript:;" onclick="moveMenu($(this),\'up\')"><i class="glyphicon glyphicon-arrow-up"></i></a> ';
		html +='<a href="javascript:;" onclick="moveMenu($(this),\'down\')"><i class="glyphicon glyphicon-arrow-down"></i></a> ';
		html +='<a href="javascript:;" onclick="delMenu($(this))"><i class="glyphicon glyphicon-remove"></i></a>';
		html +='</td></tr>';
		$(".table-menu tbody tr[main-index="+mainindex+"]").after(html);
		sehelctonbin();
	}
	//删除子菜单
	function delMenu(o){
		var class_ty = $(o).parent().parent().attr('class');
		if(class_ty == 'sub'){
			$(o).parent().parent().remove();
			var mainindex = $(o).parent().parent().attr('parent-index');
			var lengh = $('tr[parent-index='+mainindex+']').length;
			if(lengh == 0){
				var html='';
				html +='<select class="form-control" name="type'+mainindex+'"  >';
				html +='<option value="click">关键字</option>';
				html +='<option value="view">链接跳转</option>';
				html +='<option value="scancode_waitmsg">扫码带提示</option>';
				html +='<option value="scancode_push">扫码推事件</option>';
				html +='<option value="pic_sysphoto">系统拍照发图</option>';
				html +='<option value="pic_photo_or_album">拍照或者相册发图</option>';
				html +='<option value="pic_weixin">微信相册发图</option>';
				html +='<option value="location_select">发送位置</option>';
				html +='</select>';
				var inputhtml ='<div class="form-inline"><input class="form-control input-medium keyword" type="text" value="" style="width:335px" >';
				/*inputhtml +=' <button onclick="SelectContractInfo(this);return false;" class="btn btn-default" id="btn'+mainindex+'" style="display:none">快捷选择</button></div>';*/
				$('tr[main-index='+mainindex+']').find('td:eq(1)').html(html);
				$('tr[main-index='+mainindex+']').find('td:eq(2)').html(inputhtml);
				sehelctonbin();
			}
		}else{
			var mainindex =$(o).parent().parent().attr('main-index');
			$(o).parent().parent().remove();
			$('tr[parent-index='+mainindex+']').remove();
		}
		
	}
	//菜单往上移动
	function moveMenu(o,type){
		var class_ty = $(o).parent().parent().attr('class');
		var tabletrlength = $(".table-menu tbody tr").length;
		var input_index = Math.floor(Math.random()*10000);
		//子菜单只能在父级里移动
		if(class_ty == 'sub'){
				var parentindex =$(o).parent().parent().attr('parent-index');
				var tindex = $(o).parent().parent().index();
				var prevnum=0;
				for(var i =0; i < tabletrlength; i++ ){
					if($(".table-menu tbody tr:eq("+i+")").attr('parent-index') == parentindex){
						prevnum++;
					}
				}
				
				if(prevnum == 0){
					return false;
				}
				
				$(o).parent().parent().addClass('subremove');
				var html="<tr class='sub' parent-index='"+parentindex+"' >";
				html +="<td><div class='sub-node'><input class='form-control input-medium name' type='text' value='"+$(".table-menu tbody tr:eq("+tindex+")").find('.input-medium.name').val()+"' /></div></td>";
				html +="<td>";
				var checked=$(".table-menu tbody tr:eq("+tindex+")").find('select option:selected').val();
				var name=$(".table-menu tbody tr:eq("+tindex+")").find('select').attr('name');
				var display,input_width;
				display = 'style="display:none"';
				input_width = ' style="display:none"';
				switch(checked){
					case 'click':
						var click ='selected="true"';
						display = 'style="display:none;"';
						input_width = ' style="width:335px;"';
					break;
					case 'view':
						var view = 'selected="true"';
						display = 'style="display:inline-block"';
						input_width = ' style="width:255px;"';
					break;
					case 'scancode_waitmsg':
						var scancode_waitmsg='selected="true"';
					break;
					case 'scancode_push':
					var scancode_push='selected="true"';
					break;
					case 'pic_sysphoto':
						var pic_sysphoto='selected="true"';
					break;
					case 'pic_photo_or_album':
						var pic_photo_or_album='selected="true"';
					break;
					case 'pic_weixin':
						var pic_weixin='selected="true"';
					break;
					case 'location_select':
						var location_select='selected="true"';
					break;
				}
				
				html +='<select class="form-control" name="'+name+'" >';
				html +='<option '+click+' value="click">关键字</option>';
				html +='<option '+view+' value="view">链接跳转</option>';
				html +='<option '+scancode_waitmsg+' value="scancode_waitmsg">扫码带提示</option>';
				html +='<option '+scancode_push+' value="scancode_push">扫码推事件</option>';
				html +='<option '+pic_sysphoto+' value="pic_sysphoto">系统拍照发图</option>';
				html +='<option '+pic_photo_or_album+' value="pic_photo_or_album">拍照或者相册发图</option>';
				html +='<option '+pic_weixin+' value="pic_weixin">微信相册发图</option>';
				html +='<option '+location_select+' value="location_select">发送位置</option>';
				html +='</select>';
				
				//html +='<label class="radio inline"><input  type="radio" name="'+name+'"  value="click" '+click+'>关键字</label>';
				//html +='<label class="radio inline"><input type="radio" name="'+name+'"  value="view" '+view+'>链接跳转</label>';
				html +="</td>";
				html +="<td class='td-keyword'><div class='form-inline'>";
				html +='<input class="form-control input-medium keyword" type="text" value="'+$(".table-menu tbody tr:eq("+tindex+")").find('.td-keyword input').val()+'"'+input_width+' />';
				/*html +='<button onclick="SelectContractInfo(this);return false;" class="btn btn-default" id="btn_'+parentindex+'_'+input_index+'" '+display+'>快捷选择</button></div>';*/
				html +="</td>";
				html +="<td><a href='javascript:;' onclick=\"moveMenu($(this),'up')\"><i class='glyphicon glyphicon-arrow-up'></i></a> ";
				html +="<a href='javascript:;' onclick=\"moveMenu($(this),'down')\"><i class='glyphicon glyphicon-arrow-down'></i></a> ";
				html +="<a href='javascript:;' onclick='delMenu($(this))'><i class='glyphicon glyphicon-remove'></i></a></td>";
				
				html +="</tr>";
				
				if(type == 'up'){
					//上移动
					if($(".table-menu tbody tr:eq("+(tindex-1)+")").attr('parent-index') != parentindex){
						$(o).parent().parent().removeClass('subremove');
						return false;
					}
					$(".table-menu tbody tr:eq("+(tindex-1)+")").before(html);
				
				}else if(type == 'down'){
					//下移动
					if($(".table-menu tbody tr:eq("+(tindex+1)+")").attr('parent-index') != parentindex){
						$(o).parent().parent().removeClass('subremove');
						return false;
					}
					$(".table-menu tbody tr:eq("+(tindex+1)+")").after(html);
				}				
				//移动后删除自己
				$(".subremove").remove();
				
		}else{
		
		//父级直接更换位置
			var mainindex =$(o).parent().parent().attr('main-index');
			var prevmainindex;
			
			var html="<tr class='main' main-index='"+mainindex+"' >";
			html +="<td><input class='form-control input-medium name' type='text' value='"+$(".main[main-index="+mainindex+"]").find('.input-medium.name').val()+"' ><a href='javascript:;' onclick='addSubMenu($(this))'><i class='glyphicon glyphicon-plus'></i> 添加二级菜单</a></td>";
			html +="<td>";
			if($(".sub[parent-index="+mainindex+"]").length == 0){
				var checked=$(".main[main-index="+mainindex+"]").find('select option:selected').val();
				var display,input_width;
				display = 'style="display:none"';
				input_width = ' style="display:none"';
				switch(checked){
					case 'click':
						var click ='selected="true"';
						display = 'style="display:none;"';
						input_width = ' style="width:335px;"';
					break;
					case 'view':
						var view = 'selected="true"';
						display = 'style="display:inline-block"';
						input_width = ' style="width:255px;"';
					break;
					case 'scancode_waitmsg':
						var scancode_waitmsg='selected="true"';
					break;
					case 'scancode_push':
					var scancode_push='selected="true"';
					case 'pic_sysphoto':
						var pic_sysphoto='selected="true"';
					break;
					case 'pic_photo_or_album':
						var pic_photo_or_album='selected="true"';
					break;
					case 'pic_weixin':
						var pic_weixin='selected="true"';
					break;
					case 'location_select':
						var location_select='selected="true"';
					break;
				}
				
				html +='<select class="form-control" name="type'+mainindex+'" >';
				html +='<option '+click+' value="click">关键字</option>';
				html +='<option '+view+' value="view">链接跳转</option>';
				html +='<option '+scancode_waitmsg+' value="scancode_waitmsg">扫码带提示</option>';
				html +='<option '+scancode_push+' value="scancode_push">扫码推事件</option>';
				html +='<option '+pic_sysphoto+' value="pic_sysphoto">系统拍照发图</option>';
				html +='<option '+pic_photo_or_album+' value="pic_photo_or_album">拍照或者相册发图</option>';
				html +='<option '+pic_weixin+' value="pic_weixin">微信相册发图</option>';
				html +='<option '+location_select+' value="location_select">发送位置</option>';
				html +='</select>';
				
				//html +='<label class="radio inline"><input  type="radio" name="type'+mainindex+'"  value="click" '+click+'>关键字</label>';
				//html +='<label class="radio inline"><input type="radio" name="type'+mainindex+'"  value="view" '+view+'>链接跳转</label>';
			}
			html +="</td>";
			html +="<td class='td-keyword'><div class='form-inline'>";
			if($(".sub[parent-index="+mainindex+"]").length == 0){
				html +='<input class="form-control input-medium keyword" type="text" value="'+$(".main[main-index="+mainindex+"]").find('.input-medium').val()+'" '+input_width+' >';
				/*html +='<button onclick="SelectContractInfo(this);return false;" class="btn btn-default" id="btn_'+input_index+'" '+display+'>快捷选择</button></div>';*/
			}
			html +="</td>";
			html +="<td><a href='javascript:;' onclick=\"moveMenu($(this),'up')\"><i class='glyphicon glyphicon-arrow-up'></i></a> ";
			html +="<a href='javascript:;'  onclick=\"moveMenu($(this),'down')\"><i class='glyphicon glyphicon-arrow-down'></i></a> ";
			html +="<a href='javascript:;' onclick='delMenu($(this))'><i class='glyphicon glyphicon-remove'></i></a></td>";
			html +="</tr>";	
			if($(".sub[parent-index="+mainindex+"]").length > 0){
					var typei=0;
					$(".sub[parent-index="+mainindex+"]").each(function(){
						typei++;
						$(this).addClass('remove');
						html +="<tr class='sub' parent-index='"+mainindex+"'>";
						html +="<td><div class='sub-node'><input class='form-control input-medium name' type='text' value='"+$(this).find('.input-medium.name').val()+"' /></div></td>";
						html +="<td>";
						var checked=$(this).find('select option:selected').val();
						var display,input_width;
						display = 'style="display:none"';
						input_width = ' style="display:none"';
						switch(checked){
							case 'click':
								var click ='selected="true"';
								display = 'style="display:none;"';
								input_width = ' style="width:335px;"';
							break;
							case 'view':
								var view = 'selected="true"';
								display = 'style="display:inline-block"';
								input_width = ' style="width:255px;"';
							break;
							case 'scancode_waitmsg':
								var scancode_waitmsg='selected="true"';
							break;
							case 'scancode_push':
								var scancode_push='selected="true"';
							break;
							case 'pic_sysphoto':
								var pic_sysphoto='selected="true"';
							break;
							case 'pic_photo_or_album':
								var pic_photo_or_album='selected="true"';
							break;
							case 'pic_weixin':
								var pic_weixin='selected="true"';
							break;
							case 'location_select':
								var location_select='selected="true"';
							break;
						}
						
						html +='<select class="form-control " name="type'+typei+"-"+mainindex+'" >';
						html +='<option '+click+' value="click">关键字</option>';
						html +='<option '+view+' value="view">链接跳转</option>';
						html +='<option '+scancode_waitmsg+' value="scancode_waitmsg">扫码带提示</option>';
						html +='<option '+scancode_push+' value="scancode_push">扫码推事件</option>';
						html +='<option '+pic_sysphoto+' value="pic_sysphoto">系统拍照发图</option>';
						html +='<option '+pic_photo_or_album+' value="pic_photo_or_album">拍照或者相册发图</option>';
						html +='<option '+pic_weixin+' value="pic_weixin">微信相册发图</option>';
						html +='<option '+location_select+' value="location_select">发送位置</option>';
						html +='</select>';
						//html +='<label class="radio inline"><input  type="radio" name="type'+typei+"-"+mainindex+'"  value="click" '+click+'>关键字</label>';
						//html +='<label class="radio inline"><input type="radio" name="type'+typei+"-"+mainindex+'"  value="view" '+view+'>链接跳转</label>';
						html +="</td>";
						html +="<td class='td-keyword'><div class='form-inline'>";
						html +='<input class="form-control input-medium keyword" type="text" value="'+$(this).find('.td-keyword input').val()+'" '+input_width+' />';
						/*html +='<button onclick="SelectContractInfo(this);return false;" class="btn btn-default" id="btn_'+input_index+'" '+display+'>快捷选择</button></div>';*/
						html +="</td>";
						html +="<td><a href='javascript:;' onclick=\"moveMenu($(this),'up')\"><i class='glyphicon glyphicon-arrow-up'></i></a> ";
						html +="<a href='javascript:;' onclick=\"moveMenu($(this),'down')\"><i class='glyphicon glyphicon-arrow-down'></i></a> ";
						html +="<a href='javascript:;' onclick='delMenu($(this))'><i class='glyphicon glyphicon-remove'></i></a></td>";
						html +="</tr>";
					});
				}
		
			
			if(type == 'up'){
				var prevcss = $(o).parent().parent().prev().attr('class');
			}else if(type == 'down'){
				if($(o).parent().parent().next().hasClass('sub')){
					var prevcss = 'sub';
				}
			}
			
			$(o).parent().parent().addClass('remove');
			//有子菜单的父级移动
			if(prevcss == 'sub'){
	
				var tindex = $(o).parent().parent().index();
				if(type == 'up'){
			
					for(var i =0; i < tindex; i++ ){
						if($(".table-menu tbody tr:eq("+i+")").attr('main-index') != undefined){
							prevmainindex = $(".table-menu tbody tr:eq("+i+")").index();
						}
					}

					if(prevmainindex == undefined){
						$(o).parent().parent().removeClass('remove');
						return false;
					}
					if($(".table-menu tbody tr:eq("+prevmainindex+")").html() == null){
						$(o).parent().parent().removeClass('remove');
						return false;
					}
					$(".table-menu tbody tr:eq("+prevmainindex+")").before(html);
					
				}else if(type =='down'){

					var _tmainindex = $(".table-menu tbody tr:eq("+tindex+")").attr('main-index');
					
					var _tsumObj = $(".table-menu tbody tr[parent-index="+_tmainindex+"]");
					
					if(_tsumObj != undefined){
						var _tsum = _tsumObj.length;
						var _nextmainindex = _tsumObj.last().next().attr('main-index');
					}else{
						var _tsum = 0;
						var _nextmainindex = _tmainindex+1;
					}
					
					var _nextmainObj = $(".table-menu tbody tr[main-index="+_nextmainindex+"]");
				
					if(_nextmainObj.html() == null){
						$(o).parent().parent().removeClass('remove');
						$(".table-menu tbody tr[parent-index="+_tmainindex+"]").removeClass('remove');
						return false;
					}
			
					var nextchildObj = $(".table-menu tbody tr[parent-index="+parseInt(_nextmainindex)+"]");
					if(nextchildObj.last().html() != null){
						nextchildObj.last().after(html);
					}else{
						_nextmainObj.after(html);
					}
				
				}
				
			}else{
			//没有子菜单的父级移动
				var pmainindex = $(o).parent().parent().index();
				
				if(type == 'up'){	
					if($(".table-menu tbody tr:eq("+(pmainindex-1)+")").html() == null){
						$(o).parent().parent().removeClass('remove');
						var _thatmainindex = $(o).parent().parent().attr('main-index');
						$(".table-menu tbody tr[parent-index="+_thatmainindex+"]").removeClass('remove');
						return false;
					}
					$(".table-menu tbody tr:eq("+(pmainindex-1)+")").before(html);
					
				}else if(type == 'down'){
				
					if($(".table-menu tbody tr:eq("+(pmainindex+1)+")").html() == null){
						$(o).parent().parent().removeClass('remove');
						return false;
					}else{
						var nextmainindex=$(".table-menu tbody tr:eq("+(pmainindex+1)+")").attr('main-index');
						var childnex = $(".table-menu tbody tr[parent-index="+nextmainindex+"]").length;
						pmainindex = parseInt(childnex)+parseInt(pmainindex);
					}
					
					$(".table-menu tbody tr:eq("+(pmainindex+1)+")").after(html);
					
				}
				
			}
			
			sehelctonbin();
			//移动后删除自己
			$(".remove").remove();
			
			return true;
		}
		
	}
	
	
	//保存提交菜单
	function saveMenu() {
		var back = '';
		if($(".table-menu tbody tr.main").length > 3) {
			back = 1;
            layer.alert('不能输入超过 3 个主菜单才能保存.',{icon: 2});
			return;
		}
		var trimvalempy=0;
		$('.input-medium').each(function(){
			var selectoptionval = $(this).parents('tr').find('select option:selected').val();
			if(selectoptionval == 'click' || selectoptionval == 'view'){
				if($.trim($(this).val()) == ''){
					trimvalempy++;
				}
			}
		});
		if(trimvalempy > 0){
			back = 1;
            layer.alert('存在未输入名称或未输入关键字的菜单.',{icon: 2});
			return;
		}
	
		$('.main').find('.input-medium.name').each(function(){
			if($(this).val().length > 5){
				back = 1;
               layer.alert('主菜单的名称长度不能超过5个字.',{icon: 2});
               return;
			}
		}); 

		
		$('.sub-node').find('.input-medium.name').each(function(){
			if($(this).val().length > 8){
				back = 1;
                layer.alert('子菜单的名称长度不能超过8个字.',{icon: 2});
				return false;
			}
		});
		
		if(back == 1){
			return false;
		}
		
		var dat = '[';
		var error = false;
		$('.table-menu tbody .main').each(function(){
			var mainindex = $(this).attr('main-index');
			var name = $.trim($(this).find('.input-medium').val()).replace(/"/g, '\"');
			var type = $(this).find('select option:selected').val();
			var keyword = $(this).find('.td-keyword input').val();
			if(!keyword) {
				keyword = '';
			}
			
			dat += '{"name": "' + name + '"';
			if($('.sub[parent-index='+mainindex+']').length > 0) {
				dat += ',"sub_button": [';
				$('.sub[parent-index='+mainindex+']').each(function(){
					var sName = $.trim($(this).find('.input-medium').val()).replace(/"/g, '\"');
					var sType = $(this).find('select option:selected').val();
					var sKeyword = $(this).find('.td-keyword input').val();
					if(!sKeyword) {
						sKeyword = '';
					}
					
					dat += '{"name": "' + sName + '"';
					if((sType == 'click' && sKeyword == '') || (sType == 'view' && !sKeyword)) {
                        layer.alert('二级菜单项 “' + sName + '”未设置对应规则.',{icon: 2});
						error = true;
						return false;
					}
					
					switch(sType){
						case 'click':
							dat += ',"type": "click","key": "' + encodeURIComponent(sKeyword) + '"';
						break;
						case 'view':
							dat += ',"type": "view","url": "' + sKeyword + '"';
						break;
						case 'scancode_waitmsg':
							dat += ',"type": "scancode_waitmsg","key": "rselfmenu_0_0"';
						break;
						case 'scancode_push':
							dat += ',"type": "scancode_push","key": "rselfmenu_0_1"';
						break;
						case 'pic_sysphoto':
							dat += ',"type": "pic_sysphoto","key": "rselfmenu_1_0"';
						break;
						case 'pic_photo_or_album':
							dat += ',"type": "pic_photo_or_album","key": "rselfmenu_1_1"';
						break;
						case 'pic_weixin':
							dat += ',"type": "pic_weixin","key": "rselfmenu_1_2"';
						break;
						case 'location_select':
							dat += ',"type": "location_select","key": "rselfmenu_2_0"';
						break;
					}
					
					dat += '},';
				});
				if(error) {
					return false;
				}
				dat = dat.slice(0,-1);
				dat += ']';
			} else {
				if((type == 'click' && keyword == '') || (type == 'view' && !keyword)) {
				    layer.alert('菜单 “' + name + '”不存在二级菜单项, 且未设置对应规则.',{icon: 2});
					error = true;
					return false;
				}
				switch(type){
					case 'click':
						dat += ',"type": "click","key": "' + encodeURIComponent(keyword) + '"';
					break;
					case 'view':
						dat += ',"type": "view","url": "' + keyword + '"';
					break;
					case 'scancode_waitmsg':
						dat += ',"type": "scancode_waitmsg","key": "rselfmenu_0_0"';
					break;
					case 'scancode_push':
						dat += ',"type": "scancode_push","key": "rselfmenu_0_1"';
					break;
					case 'pic_sysphoto':
						dat += ',"type": "pic_sysphoto","key": "rselfmenu_1_0"';
					break;
					case 'pic_photo_or_album':
						dat += ',"type": "pic_photo_or_album","key": "rselfmenu_1_1"';
					break;
					case 'pic_weixin':
						dat += ',"type": "pic_weixin","key": "rselfmenu_1_2"';
					break;
					case 'location_select':
						dat += ',"type": "location_select","key": "rselfmenu_2_0"';
					break;
				}
			}
			dat += '},';
		});
		if(error) {
			return;
		}
		dat = dat.slice(0,-1);
		dat += ']';
		//console.log(dat);
		$('#do').val(dat);
		var url = $("#form").attr('action');
		$.post(url,{'do':dat},function(r){
		  	layer.alert(r.info,{icon: 1},function(){
		  	           location.href=r.url;
            });
		})
		return false;
	}



function sehelctonbin(){
	$("select").on('change',function(){
		switch($(this).val()){
			case 'click':
				$(this).parents('tr').find('.td-keyword input').val('').show();
				$(this).parents('tr').find('button').hide();
				$(this).parents('tr').find('.td-keyword input').css('width', '335px');
			break;
			case 'view':
				$(this).parents('tr').find('.td-keyword input').val('http://').show();
				$(this).parents('tr').find('button').show();
				$(this).parents('tr').find('.td-keyword input').css('width', '255px');
			break;
			default:
				$(this).parents('tr').find('.td-keyword input').val('');
				$(this).parents('tr').find('button').hide();
				$(this).parents('tr').find('.td-keyword input').hide();
			break;
		}
	});
}


function changeappid(){
	var appid = $("#appid").val();
	var appsecret = $("#appsecret").val();
	if(appid == '' || appsecret == ''){
		return false;
	}
	$.post("<{:U('Wechats/MenuDisplay')}>",{appid:appid,appsecret:appsecret},function(e){
	       	layer.alert('操作成功！',{icon: 1},function(){	       	   
                    window.location.reload();
	       	});
			$(".view_appicandsecret").show();
			$(".changeappid").hide();
	});
}	
function changap(){

	layer.confirm('更换AppId和AppSecret会导致自定义菜单丢失，确定修改？', function(index){
	    $(".view_appicandsecret").hide();
		$(".changeappid").show();
	    layer.close(index);
	});  

}
</script>


<div class="container app">
	<include file="Public/sidebar" /> 
	<div id="mainCon" class="mainCon">
			
		<h4>自定义菜单设置</h4>
		<div class="bk10"></div>

			<div style='margin-bottom:10px;padding: 10px;'>
				<span class='changeappid' style="<if condition = "$account['appid'] and $account['appsecret']">display:none</if>">
					<span>AppId： <input type='text' class='span2 inputStyle' id='appid'  value="<{$account['appid']}>" name='key' style="width:202px;" /></span>
					<span>AppSecret： <input type='text' class='span3 inputStyle' id='appsecret'   value="<{$account['appsecret']}>"  name='secret' style="width:202px;"/></span>
				</span>
				<input type='button' style="<if condition = "$account['appid'] and $account['appsecret']">display:none</if>" onclick='changeappid()' value='保存' class='btn btn-primary changeappid'/>
				<if condition = "$account['appid'] and $account['appsecret']">
				<span class='view_appicandsecret'>
					<span>AppId：<input type='text' class='span2 inputStyle' readonly="true" id='appid' value="<{$account['appid']}>" name='key'  style="width:202px;" /></span>
					<span>AppSecret： <input type='text' class='span3 inputStyle' readonly="true" id='appsecret'  value="<{$account['appsecret']}>"  name='secret'  style="width:202px;" /></span>
				</span>
				<a href='javascript:' onclick='changap()'  class='btn btn-default view_appicandsecret'>修改</a>
				</if>
			</div>


			<if condition = "$account['appid'] and $account['appsecret']">
			<div style='margin-bottom:10px;'>
				<div class='alert alert-warning'>请注意:扫码带提示、扫码推事件、系统拍照发图、拍照或者相册发图、微信相册发图、发送位置事件，仅支持微信iPhone5.4.1以上版本，和Android5.4以上版本的微信用户，旧版本微信用户点击后将没有回应。
				<br/>
				<p>保存当前菜单设置至公众平台, 由于缓存可能需要在24小时内生效。</p></div>
			</div>

			<div class="form form-horizontal">
				<table class="table table-hover table-bordered table-menu">
					<thead class="navbar-inner">
						<tr>
							<th style='width:200px;'>菜单名称</th>
							<th>类型</th>
							<th>触发关键词或外链URL</th>
							<th></th>
						</tr>
					</thead>
					<tbody>
			
					<?php $a=0;?>
					<volist name="menus['menu']['button']" id="row">
						<?php $a++;?>
						<tr class="main" main-index="<{$a}>">
							<td>
								<input class="form-control input-medium name" type="text" value="<{$row['name']}>">
								<a class="btn btn-small" onclick="addSubMenu($(this))"><i class="glyphicon glyphicon-plus"></i> 添加二级菜单</a>
							</td>
							<td>
								<empty name="row['sub_button']">
								<select class="form-control" name="typerow<{$a}>" >
									<option <eq name="row['type']" value="click" >selected="true" </eq> value="click">关键字</option>
									<option <eq name="row['type']" value="view" >selected="true" </eq> value="view">链接跳转</option>
									<option <eq name="row['type']" value="scancode_waitmsg" >selected="true" </eq> value="scancode_waitmsg">扫码带提示</option>
									<option <eq name="row['type']" value="scancode_push" >selected="true" </eq> value="scancode_push">扫码推事件</option>
									<option <eq name="row['type']" value="pic_sysphoto" >selected="true" </eq> value="pic_sysphoto">系统拍照发图</option>
									<option <eq name="row['type']" value="pic_photo_or_album" >selected="true" </eq> value="pic_photo_or_album">拍照或者相册发图</option>
									<option <eq name="row['type']" value="pic_weixin" >selected="true" </eq> value="pic_weixin">微信相册发图</option>
									<option <eq name="row['type']" value="location_select" >selected="true" </eq> value="location_select">发送位置</option>
								</select>
								</empty>
							</td>
							<td class="td-keyword">
								<empty name="row['sub_button']">
								<div class="form-inline">
								<input type="text"  class="form-control input-medium keyword" value="<eq name="row['type']"  value="click"><{$row['forward']}><else/><{$row['url']}></eq>"  <switch name="row.type"><case value="click">style="width:335px;"</case><case value="view">style="width:252px;"</case><default />style="display:none"</switch>  />
								<!--<button onclick="SelectContractInfo(this);return false;" class="btn btn-default" id="btn<{$a}>" <neq name="row['type']" value="view">style="display:none"</neq>>快捷选择</button>-->
								</div>
								</empty>
							</td>
							<td>
								<a href='javascript:;' onclick="moveMenu($(this),'up')"><i class='glyphicon glyphicon-arrow-up'></i></a> 
								<a href='javascript:;' onclick="moveMenu($(this),'down')"><i class='glyphicon glyphicon-arrow-down'></i></a> 
								<a href='javascript:;'  onclick="delMenu($(this))"><i class="glyphicon glyphicon-remove"></i></a>
							</td>
						</tr>
						<notempty name="row['sub_button']">
						<?php $s=0;?>
						<volist name="row['sub_button']" id="btn">
						<?php $s++;?>
							<tr class="sub" parent-index="<{$a}>">
								<td>
									<div class="sub-node">
										<input class="form-control input-medium name" type="text" value="<{$btn['name']}>">
									</div>
								</td>
								<td>
									<select class="form-control" name="typerow<{$s}>-<{$a}>" >
										<option <eq name="btn['type']" value="click" >selected="true" </eq> value="click">关键字</option>
										<option <eq name="btn['type']" value="view" >selected="true" </eq> value="view">链接跳转</option>
										<option <eq name="btn['type']" value="scancode_waitmsg" >selected="true" </eq> value="scancode_waitmsg">扫码带提示</option>
										<option <eq name="btn['type']" value="scancode_push" >selected="true" </eq> value="scancode_push">扫码推事件</option>
										<option <eq name="btn['type']" value="pic_sysphoto" >selected="true" </eq> value="pic_sysphoto">系统拍照发图</option>
										<option <eq name="btn['type']" value="pic_photo_or_album" >selected="true" </eq> value="pic_photo_or_album">拍照或者相册发图</option>
										<option <eq name="btn['type']" value="pic_weixin" >selected="true" </eq> value="pic_weixin">微信相册发图</option>
										<option <eq name="btn['type']" value="location_select" >selected="true" </eq> value="location_select">发送位置</option>
									</select>
								</td>
								<td class="td-keyword">
									<div class="form-inline">
									<input type="text" class="form-control input-medium keyword" value="<eq name="btn['type']"  value="click"><{$btn['forward']}><else/><{$btn['url']}></eq>" <switch name="btn.type"><case value="click">style="width:335px;"</case><case value="view">style="width:252px;"</case><default />style="display:none"</switch> />
									<!--<button onclick="SelectContractInfo(this);return false;" class="btn btn-default" id="btn_<{$a}>_<{$s}>" <neq name="btn['type']" value="view">style="display:none"</neq>>快捷选择</button>-->
									</div>
								</td>
								<td class="text-center">
									<a href='javascript:;' onclick="moveMenu($(this),'up')"><i class='glyphicon glyphicon-arrow-up'></i></a>
									<a href='javascript:;' onclick="moveMenu($(this),'down')"><i class='glyphicon glyphicon-arrow-down'></i></a>
									<a href='javascript:;' onclick="delMenu($(this))"><i class="glyphicon glyphicon-remove"></i></a>
								</td>
							</tr>
						</volist>
						</notempty>
					</volist>
					</tbody>
				</table>
				
				<div class="bk10"></div>
				<div class="form-actions text-center">
					<a href="javascript:;" onclick="addMenu();" class="btn btn-default"><i class="glyphicon glyphicon-plus"></i> 添加菜单</a>
					<input type="button" value="保存菜单设置" class="btn btn-danger span2" onclick="saveMenu();"/>
				</div>
			</div>

				
			<else/>
				<div class='alert alert-warning'>请正确填写AppId和AppSecret才能正常使用自定义菜单功能</div>
			</if>
	</div>

<form action="<{:U('Wechats/MenuPost')}>" method="post" id="form"><input id="do" name="do" type="hidden" /></form>

<script>
$(function(){
	$("select").on('change',function(){
		switch($(this).val()){
			case 'click':
				$(this).parents('tr').find('.td-keyword input').val('').show();
				$(this).parents('tr').find('button').hide();
				$(this).parents('tr').find('.td-keyword input').css('width', '335px');
			break;
			case 'view':
				$(this).parents('tr').find('.td-keyword input').val('http://').show();
				$(this).parents('tr').find('button').show();
				$(this).parents('tr').find('.td-keyword input').css('width', '255px');
			break;
			default:
				$(this).parents('tr').find('.td-keyword input').val('');
				$(this).parents('tr').find('button').hide();
				$(this).parents('tr').find('.td-keyword input').hide();
			break;
		}
	});
});

</script>
<style>
.table-striped td{padding-top: 10px;padding-bottom: 10px}
a{font-size:14px;}
a:hover, a:active{text-decoration:none; color:red;}
.hover td{padding-left:10px;}
.sub-node{padding-left:50px;background:url('__PUBLIC__/Application/images/wechats_mewnu_bg_repno.gif') no-repeat -245px -545px;}
.table tbody tr td:first-child {text-align: left;}

.btn{	
	margin-left:5px;
}
.table-menu .main td:nth-last-child(3){
	width:20%
}
.table-menu .main td:nth-last-child(3) select {
	width:80%
}
.table-menu .sub td:nth-last-child(3){
	width:20%
}
.table-menu .sub td:nth-last-child(3) select {
	width:100%
}
.table-menu .main td:nth-last-child(4){
	width:28%
}
.table-menu .sub td:nth-last-child(2) input{
	width:251px
}
.inputStyle{
height: 34px;
  padding: 6px 12px;
  font-size: 14px;
  line-height: 1.42857143;
  color: #555;
  background-color: #fff;
  background-image: none;
  border: 1px solid #ccc;
  border-radius: 4px;
  -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
  box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
  -webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
  -o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
  transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
}
</style>
